<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright  Copyright (c) 2006-2008, Julien PORTALIER
 * @link       http://feather-cm.googlecode.com/
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

#$filePath = preg_replace('~^/~', '', $_GET['url']);
$filePath = ltrim($_GET['url'], '/');
$resolutions = array(
	'icon'      => 35,
	'square'    => 75,
	'thumbnail' => 100,
	'small'     => 240,
	'medium'    => 500,
	'large'     => 1024
);

if (!file_exists($filePath))
{
	// not found
	header("HTTP/1.1 404 Not Found");
	exit;
}
elseif (!isset($_GET['size']) or !isset($resolutions[$_GET['size']]))
{
	// not asked for a size? redirection to original file
	header('HTTP/1.1 301 Moved Permanently');
	header("Location: /$filePath");
	exit;
}

// browser cache (1 day).
header("Expires: ".gmdate('D, j M Y H:i:s', time() + 3600 * 24)." GMT");
header("Cache-Control: cache");	// HTTP/1.1
header("Pragma: cache");		// HTTP/1.0

// server thumbnail's cache
$fileCache = '../tmp/cache/thumbnails/'.md5($_GET['size'].'-'.$filePath);

if (!file_exists($fileCache) or filemtime($filePath) > filemtime($fileCache))
{
	require '../../feather2/libs/resampler.php';
	$resampler = new Resampler($filePath);
	$max = $resolutions[$_GET['size']];
	$resampler->create($fileCache, $max, $max, ($_GET['size'] == 'square' or $_GET['size'] == 'icon') ? true : false);
}

// returns thumbnail
header('Content-Type: image/jpeg');
$data = readfile($fileCache);
?>